package com.cac.demo.newCode.interfaceLog.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cac.demo.newCode.interfaceLog.entity.InterFaceVO;
import com.cac.demo.newCode.interfaceLog.entity.InterfaceLog;
import com.cac.demo.newCode.interfaceLog.mapper.InterfaceLogMapper;
import com.cac.demo.newCode.interfaceLog.service.InterfaceLogService;
import com.cac.demo.newCode.utils.Constants;
import com.cac.demo.util.adapter.DaoException;
import com.cac.demo.util.adapter.QueryReqBean;
import com.cac.demo.util.adapter.QueryRespBean;
import com.cac.demo.util.adapter.ResponseMsg;
import com.github.pagehelper.PageHelper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class InterfaceLogServiceImpl extends ServiceImpl<InterfaceLogMapper, InterfaceLog> implements InterfaceLogService {


    @Resource
    private InterfaceLogService interfaceLogService;

    @Override
    public void saveLog(InterfaceLog interfaceLog) {
        try {
            this.save(interfaceLog);
        } catch (Exception e) {
            log.error("保存接口日志失败", e);
        }
    }

    @Override
    public void saveInterfaceLog(String description, String url, Object list, Object result, String ip, String method,String bKey) {
        try {
            InterfaceLog interfaceLog = new InterfaceLog();
            interfaceLog.setDescription(description);
            interfaceLog.setUrl(url);
            interfaceLog.setMethod(method);
            interfaceLog.setParams(list == null ? "null" : list.toString());
            interfaceLog.setResponse(result == null ? "null" : result.toString());
            interfaceLog.setIp(ip);
            interfaceLog.setUserAgent(description == null ? "unknown" : description);
            interfaceLog.setStatus(0);
            interfaceLog.setCreateTime(new Date());
            interfaceLog.setBKey(bKey);
            interfaceLog.setIsResult(0);
            this.save(interfaceLog);
        }
        catch (Exception e) {
            log.error("保存接口日志失败", e);
        }
    }
    //协调单查询
    @Override
    public ResponseMsg<Object> getInterFaceList(Map map) {
        try {
            // 获取并验证时间参数
            String startTimeStr = (String) map.get("createStartDate");
            String endTimeStr = (String) map.get("createEndDate");

            Date startTime = null;
            Date endTime = null;

            if (startTimeStr != null && !startTimeStr.isEmpty()) {
                startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTimeStr);
            }

            if (endTimeStr != null && !endTimeStr.isEmpty()) {
                endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTimeStr);
            }
            LambdaQueryWrapper<InterfaceLog> queryWrapper = new LambdaQueryWrapper<>();
            if (map.get("bKey") != null && !map.get("bKey").equals("")) {
                queryWrapper.like(InterfaceLog::getBKey, map.get("bKey"));
            }
            if (map.get("description") != null && !map.get("description").equals("")) {
                queryWrapper.like(InterfaceLog::getDescription, map.get("description"));
            }
            if (map.get("url") != null && !map.get("url").equals("")) {
                queryWrapper.like(InterfaceLog::getUrl, map.get("url"));
            }
            if (map.get("status") != null && !map.get("status").equals("")) {
                queryWrapper.eq(InterfaceLog::getStatus, map.get("status"));
            }
            if (map.get("isResult") != null && !map.get("isResult").equals("")) {
                queryWrapper.like(InterfaceLog::getIsResult, map.get("isResult"));
            }
            if (startTime != null && endTime != null) {
                queryWrapper.between(InterfaceLog::getCreateTime, startTime, endTime);
            } else if (startTime != null) {
                queryWrapper.ge(InterfaceLog::getCreateTime, startTime);
            } else if( endTime != null) {
                queryWrapper.le(InterfaceLog::getCreateTime, endTime);
            }
            queryWrapper.select().orderByDesc(InterfaceLog::getCreateTime);
            // 分页处理
            int current = (int) map.get("current");
            int size = (int) map.get("size");
            Page<InterfaceLog> pagea = new Page<>(current, size);
            Page<InterfaceLog> page =  page(pagea,queryWrapper);
            HashMap<Object, Object> mapr = new HashMap<>();
            mapr.put("pageTotal", page.getTotal());
            mapr.put("size", size);
            mapr.put("current", current);
            mapr.put("data", page.getRecords());
            return new ResponseMsg<>().setResponseBody(mapr).setRetCode(Constants.SUCCESS_CODE).setMessage(Constants.SUCCESS_MESSAGE);
        } catch (Exception e) {
            throw new DaoException(e.getMessage());
        }
    }

}
